#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2011-2025 OpenFOAM Foundation
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     Alltest
#
# Description
#     quickly tests the tutorials
#
#------------------------------------------------------------------------------
cd "${0%/*}" || exit 1 # Run from this directory

usage()
{
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

usage: ${0##*/} [OPTION]

options:
  -from <dir>       specify directory to run tests from
  -to <dir>         specify directory to run tests in
  -help             print the usage

* quickly tests the tutorials

USAGE
    exit 1
}

#------------------------------------------------------------------------------

currentDir="$(pwd)"
fromDir="./"
toDir=../tutorialsTest


# Parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    -f | -from)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        fromDir="$2"
        shift
        ;;
    -t | -to)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        toDir="$2"
        shift
        ;;
    -h | -help)
        usage
        ;;
    -*)
        usage "unknown option: '$*'"
        ;;
    *)
        usage "unknown option/argument: '$*'"
        ;;
    esac
    shift
done

if [ $(readlink -f $fromDir) != $(readlink -f $toDir) ]
then
    # Create a copy of the cases for which to run the test loop
    echo "Copying cases"
    if [ -d "$toDir" ]
    then
        rm -rf "$toDir" || exit 1
    fi
    cp -a "${fromDir}" "${toDir}" || exit 1
fi

# Change the control dict to make the tests quick
echo "Modifying the controlDicts to run only one time step"
cd ${toDir} || exit 1
find . -name "controlDict*" | while read -r controlDict
do
    (
        foamDictionary -entry startFrom -set latestTime "$controlDict"
        foamDictionary -entry stopAt -set nextWrite "$controlDict"
        foamDictionary -entry writeControl -set timeStep "$controlDict"
        foamDictionary -entry writeInterval -set 1 "$controlDict"

        # Generate the system/configDict files except for controlDict.orig
        if [ $(basename "$controlDict") == "controlDict" ]
        then
           configDict="$(dirname "$controlDict")"/configDict
           foamDictionary -entry DebugSwitches \
               -merge "{ fvSchemes 1; solution 1; }" -create "$configDict"
        fi
    ) > /dev/null
done

# Copy the Allrun script into the test directory
if [ ! -x "$toDir"/Allrun ]
then
    if [ -x "$currentDir"/Allrun ]
    then
        cp -f "$currentDir"/Allrun . || exit 1
    else
        cp -f "$FOAM_TUTORIALS"/Allrun . || exit 1
    fi
fi

# Run everything
./Allrun && exit 0 || exit 1

#------------------------------------------------------------------------------
